package com.zw.yzk.kabu.skin;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;
import com.zw.yzk.kabu.R;
import skin.support.app.SkinLayoutInflater;

import java.lang.reflect.Field;

/**
 * Created by skydar on 2017/4/15.
 */

public class SkinCheckBoxInflater implements SkinLayoutInflater {
    @Override
    public View createView(@NonNull Context context, String name, @NonNull AttributeSet attrs) {
        View view = null;
        switch (name) {
            case "SkinCheckBox":
                int id = 0;
                try {
                    Class clasz = Class.forName("com.android.internal.R$styleable");
                    Field field = clasz.getDeclaredField("View");
                    field.setAccessible(true);
                    int[] styleable = (int[])field.get(null);
                    TypedArray array = context.obtainStyledAttributes(attrs, styleable);

                    // Resources.getSystem().getIdentifier("View_id", "attr", "android")
                    field = clasz.getDeclaredField("View_id");
                    field.setAccessible(true);
                    int attrId = (Integer)field.get(null);
                    id = array.getResourceId(attrId, 0);
                    array.recycle();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                if(id == 0) break;
                if(id == R.id.cos_page || id == R.id.exhibition_page || id == R.id.mall_page || id == R.id.me_page){
                    view = new SkinCheckBox(context, attrs);
                }
                break;
        }
        return view;
    }
}
